<?php
abstract class Messages {

    private static $state           = true,
                   $initialized     = array(),
                   $messages        = array(),
                   $messagesReaded  = false,
                   $page            = 1,
                   $pageLimit       = 10,
                   $limit           = '',
                   $table           = 'messages';

    public  static $query    = '',
                   $types    = array('min'=>1, 'max'=>2),
                   $numRows  = 0;



    private function pageToLimit() {
        self::$page  = (self::$page > 0) ? self::$page : 1;
        self::$limit = sprintf('LIMIT %d, %d', (self::$page - 1) * self::$pageLimit, self::$pageLimit);
    }

    private static function init() {

        if(self::$messagesReaded) return true;
        self::pageToLimit();
        self::$query = sprintf("SELECT SQL_CALC_FOUND_ROWS * FROM %s ORDER BY `to_date` DESC %s", self::$table, self::$limit);
        if (!$res = DB::query(self::$query)) {
            return false;
        }
        while ($row = $res->fetch_array(MYSQLI_ASSOC)){
            self::$messages[self::$page][$row['id']] = new Message($row);
        }
        $res = DB::query('SELECT FOUND_ROWS()');
        self::$numRows = Arr::getValue($res->fetch_array(MYSQLI_NUM), 0, 0);
        self::$messagesReaded = true;
        return true;
    }


    private static function reset() {
        self::$state          = true;
        self::$initialized    = array();
        self::$messages       = array();
        self::$page           = 1;
        self::$pageLimit      = 10;
        self::$limit          = '';
        self::$query          = '';
        self::$numRows        = 0;
        self::$messagesReaded = false;
    }

    public static function order($messages) {
        $sorted = array();
        foreach ($messages as $value) {
            array_push($sorted, $value);
        }
        return $sorted;
    }

    public static function get($p = 1, $plimit = 10, $reset = false) {
        if ($reset) self::reset();
        if (!self::$state) return false;
        if(Arr::getValue(self::$initialized, $p, false)) return true;
        self::$page = $p;
        if(!self::init()) return false;
        
        self::$pageLimit = $plimit;
        if(!self::init()) return false;
        self::$initialized[$p] = true;

        return self::$messages[$p];
    }

    public static function getByID($id) {
        $parent = Arr::getValue(self::$items, $id, false);
        if ($parent) return $parent;

        foreach (self::$items as $parent) {
            $c = Arr::getValue($parent->children, $id, false);
            if ($c) return $c;
        }
        return false;
    }
}


class Message {

    public $id       = 0,
           $title    = '',
           $text     = '',
           $viewable = 0,
           $type     = 1,
           $to_date  = 0;

    public function __construct($array) {
        if (!is_array($array)) return;
        foreach ($array as $key => $val)  {
            $this->$key = $val;
        }
    }

}
?>
